// shieldsio-generator.js
class ShieldsIOGenerator {
    constructor(baseUrl = 'https://img.shields.io') {
        this.baseUrl = baseUrl.replace(/\/$/, '');
        this.colors = {
            brightgreen: 'brightgreen',
            green: 'green',
            yellowgreen: 'yellowgreen',
            yellow: 'yellow',
            orange: 'orange',
            red: 'red',
            blue: 'blue',
            lightgrey: 'lightgrey',
            grey: 'grey'
        };
        this.styles = {
            flat: 'flat',
            flatSquare: 'flat-square',
            plastic: 'plastic',
            forTheBadge: 'for-the-badge',
            social: 'social'
        };
    }

    encodeText(text) {
        return encodeURIComponent(text.replace(/ /g, '_'));
    }

    addQueryParams(url, params) {
        if (!params || Object.keys(params).length === 0) {
            return url;
        }
        const queryString = Object.entries(params)
            .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
            .join('&');
        return `${url}?${queryString}`;
    }

    staticBadge({
        label,
        message,
        color = 'blue',
        labelColor,
        style = 'flat',
        logo,
        logoColor,
        logoSize,
        logoWidth
    }) {
        const labelEncoded = this.encodeText(label);
        const messageEncoded = this.encodeText(message);
        let url = `${this.baseUrl}/badge/${labelEncoded}-${messageEncoded}-${color}`;

        const params = {};
        if (labelColor) params.labelColor = labelColor;
        if (style && style !== 'flat') params.style = style;
        if (logo) params.logo = logo;
        if (logoColor) params.logoColor = logoColor;
        if (logoSize) params.logoSize = logoSize;
        if (logoWidth) params.logoWidth = logoWidth.toString();

        return this.addQueryParams(url, params);
    }

    dynamicJsonBadge({
        url: apiUrl,
        query,
        label,
        color,
        prefix,
        suffix,
        cacheSeconds,
        default: defaultValue,
        style = 'flat'
    }) {
        const baseUrl = `${this.baseUrl}/badge/dynamic/json`;
        const params = {
            url: apiUrl,
            query,
            label
        };

        if (color) params.color = color;
        if (prefix) params.prefix = prefix;
        if (suffix) params.suffix = suffix;
        if (cacheSeconds !== undefined) params.cacheSeconds = cacheSeconds.toString();
        if (defaultValue) params.default = defaultValue;
        if (style && style !== 'flat') params.style = style;

        return this.addQueryParams(baseUrl, params);
    }

    // GitHub徽章快捷方法
    githubStars(user, repo) {
        return `${this.baseUrl}/github/stars/${user}/${repo}`;
    }

    githubIssues(user, repo) {
        return `${this.baseUrl}/github/issues/${user}/${repo}`;
    }

    githubLicense(user, repo) {
        return `${this.baseUrl}/github/license/${user}/${repo}`;
    }

    npmVersion(packageName) {
        return `${this.baseUrl}/npm/v/${packageName}`;
    }
}

// 使用示例
function exampleUsage() {
    const generator = new ShieldsIOGenerator();

    console.log('=== 静态徽章示例 ===');
    
    const staticBadge = generator.staticBadge({
        label: '版本',
        message: '1.0.0',
        color: 'brightgreen'
    });
    console.log('基础徽章:', staticBadge);

    const logoBadge = generator.staticBadge({
        label: 'Python',
        message: '3.8',
        color: 'blue',
        logo: 'python',
        logoColor: 'white'
    });
    console.log('带Logo徽章:', logoBadge);

    console.log('\n=== 动态徽章示例 ===');
    
    const dynamicBadge = generator.dynamicJsonBadge({
        url: 'https://api.github.com/repos/vuejs/vue',
        query: '$.stargazers_count',
        label: 'Stars',
        color: 'gold',
        prefix: '⭐',
        cacheSeconds: 3600
    });
    console.log('动态徽章:', dynamicBadge);

    console.log('\n=== GitHub徽章示例 ===');
    console.log('Stars:', generator.githubStars('vuejs', 'vue'));
    console.log('NPM:', generator.npmVersion('react'));

    return {
        staticBadge,
        logoBadge,
        dynamicBadge
    };
}

// 导出供其他模块使用
if (typeof module !== 'undefined' && module.exports) {
    module.exports = ShieldsIOGenerator;
} else {
    // 浏览器环境
    window.ShieldsIOGenerator = ShieldsIOGenerator;
}

// 运行示例
exampleUsage();